//
//  OMGoodDetailRecommendTableCell.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/19.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMGoodDetailRecommendTableCell.h"
#import "OMGoodDetailRecommendCollectCell.h"
@interface OMGoodDetailRecommendTableCell () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UILabel *elementLabel;
@property (nonatomic, strong) UICollectionView *collectView;
@property (nonatomic, strong) NSArray *goods;
@end
@implementation OMGoodDetailRecommendTableCell
#define kBarHeight SM_SIZE(90)
#define kBarMarginTop SM_SIZE(20)
- (void)configure
{
    [super configure];
    self.contentView.backgroundColor = SM_backGroundColor;

    _elementLabel = [UILabel new];
    _elementLabel.font = SM_CFont(30);
    [self.contentView addSubview:_elementLabel];
    _elementLabel.backgroundColor = SM_whiteColor;
    _elementLabel.text = @"    您可能还想买";
    [_elementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(@(kBarMarginTop));
        make.height.equalTo(@(kBarHeight));
    }];


    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero
                                     collectionViewLayout:[self layout]];
    _collectView.delegate = self;
    _collectView.dataSource = self;
    _collectView.backgroundColor = SM_whiteColor;
    [self.contentView insertSubview:_collectView belowSubview:_elementLabel];
    [_collectView registerClass:[OMGoodDetailRecommendCollectCell class]
     forCellWithReuseIdentifier:[OMGoodDetailRecommendCollectCell cellIdentifier]];
    _collectView.scrollEnabled = NO;
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_elementLabel.mas_bottom);
        make.bottom.right.left.equalTo(@0);
    }];


    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = SM_naviShadowColor;
    [self.contentView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.height.equalTo(@.5f);
    }];

    UIView *topLine = [UIView new];
    topLine.backgroundColor = SM_naviShadowColor;
    [self.contentView addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.bottom.equalTo(self.elementLabel.mas_bottom);
        make.height.equalTo(@.5f);
    }];
}

- (void)configureCellWithObj:(id)obj
{
    self.goods = obj;
}

- (void)setGoods:(NSArray *)goods
{
    NSArray *oldValue = _goods;
    _goods = goods;
    oldValue==goods?:[self.collectView reloadData];
}


#define kMarginVertical SM_SIZE(0)
#define kMarginHorizional SM_SIZE(0)
#define kItemSpacing kMarginVertical
#define kItemSize ((SM_SCREEN_WIDTH - kItemSpacing*2 - kMarginHorizional*2)/3)
#define kItemHeight (kItemSize*1.6)
- (UICollectionViewLayout*)layout
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = kMarginVertical;
    flowLayout.minimumLineSpacing = kMarginVertical;
    flowLayout.itemSize = CGSizeMake(kItemSize, kItemHeight);
    flowLayout.sectionInset = UIEdgeInsetsMake(kMarginVertical, kMarginHorizional, kMarginVertical, kMarginHorizional);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    return flowLayout;
}

#pragma mark - collectionView delegate && datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _goods.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OMGoodDetailRecommendCollectCell *cell = [OMGoodDetailRecommendCollectCell collectCellWithCollectionView:collectionView
                                                                   atIndexPath:indexPath];
    OMFakeGoodData *good = self.goods[indexPath.item];
    [cell configureCellWithObj:good];
    cell.delegate = self.delegate;
    cell.contentView.backgroundColor = SM_whiteColor;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NaviPush(@"OMGoodDetailViewController", @{@"productId":[self.goods[indexPath.item] id]});
}


+ (CGFloat)cellHeightWithItems:(NSArray *)items
{

    return kBarHeight+kBarMarginTop+(kItemHeight+kItemSpacing)*[self lineCountWithNum:items.count]+kItemSpacing;
}


+ (NSUInteger)lineCountWithNum:(NSUInteger)num
{
    return num/3+(num%3==0?0:1);
}

@end
